package com.haoniu.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Properties;


/**
 * 读取配置文件，更新了UTF-8中文乱码问题
 * @author Administrator
 */
public class SysPropUtils {
	static String filename = "sys.properties";
	private static Properties sysPropUtilsties;
	static SysPropUtils sysPropUtils = null;

	/**
	 * 获取一个工程
	 * 
	 * @return Properties
	 */
	@SuppressWarnings("static-access")
	public static Properties getProp() {
		if (sysPropUtilsties == null) {
			sysPropUtilsties = sysPropUtils.getInstance().getProp();
		}
		return sysPropUtilsties;
	}

	/**
	 * 在工程文件中获取filename的值
	 * 
	 * @param propName
	 * @return String
	 */
	@SuppressWarnings("static-access")
	public static String getPropValue(String propName) {
		if (sysPropUtilsties == null)
			sysPropUtilsties = sysPropUtils.getInstance().getProp(filename);
		String value = sysPropUtilsties.getProperty(propName);

		return value;
	}

	/**
	 * 在工程文件中获取filename的值，并以整形返回
	 * 
	 * @param propName
	 * @return int
	 */
	@SuppressWarnings("static-access")
	public static int getIntPropValue(String propName) {
		if (sysPropUtilsties == null)
			sysPropUtilsties = sysPropUtils.getInstance().getProp(filename);
		int result = 0;
		try {
			result = Integer.parseInt(sysPropUtilsties.getProperty(propName));
		} catch (NumberFormatException e) {
			System.err.println("SysProp.getIntPropValue(String).NumberFormatException error : " + e);
		}
		return result;
	}

	/**
	 * 在工程文件中获取filename的值，并以布尔型返回
	 * 
	 * @param propName
	 * @return boolean
	 */
	public static boolean getBlnPropValue(String propName) {
		String temp = getPropValue(propName);
		if (temp != null && temp.equals("true"))
			return true;
		else
			return false;
	}

	/**
	 *　获取指定文件下的指定参数的值
	 * 
	 * @param propName
	 * @param fileName
	 * @return String
	 */
	@SuppressWarnings("static-access")
	public static String getPropValue(String propName, String fileName) {
		Properties prop = sysPropUtils.getInstance().getProp(fileName);
		return prop.getProperty(propName);
	}

	/**
	 * 获取一个工程并实例化
	 * 
	 * @return SysProp
	 */
	protected static SysPropUtils getInstance() {
		if (sysPropUtils == null)
			sysPropUtils = new SysPropUtils();
		return sysPropUtils;
	}

	/**
	 * 通过指定的文件名来实现一个工程类
	 * 
	 * @param filename
	 * @return Properties
	 */
	@SuppressWarnings({ "rawtypes" })
	public static Properties getPropbyFileName(String filename) {
		Properties props;
		props = new Properties();
		try {
			InputStream is = new FileInputStream(new File(filename));
			props.load(is);
			is.close();
		} catch (Exception e) {
			System.err.println("Can't read the properties file. Make sure "
					+ filename + " is in the CLASSPATH");
			return null;
		}
		Enumeration enu = props.propertyNames();
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			props.setProperty(name, props.getProperty(name));
		}
		return props;
	}

	/**
	 * 通过指定的资源文件名来实现一个工程类
	 * 
	 * @param filename
	 * @return Properties
	 */
	@SuppressWarnings({ "rawtypes" })
	public static Properties getPropbyResourceName(String filename) {
		Properties props;
		InputStream is = new SysPropUtils().getClass().getClassLoader()
				.getResourceAsStream(filename);
		props = new Properties();
		try {
			props.load(is);
			is.close();
		} catch (Exception e) {
			System.err.println("Can't read the properties file. Make sure "
					+ filename + " is in the CLASSPATH");
			return null;
		}
		Enumeration enu = props.propertyNames();
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			props.setProperty(name, props.getProperty(name));
		}
		return props;
	}

	/**
	 * 通过文件名来实例化一个工程类。filename必须在conf
	 * 
	 * @param filename
	 * @return Properties
	 */
	@SuppressWarnings({ "rawtypes" })
	private Properties getProp(String filename) {
		Properties props;
		InputStream is = getClass().getClassLoader().getResourceAsStream(filename);
		props = new Properties();
		try {
			props.load(new InputStreamReader(is, "gbk"));
			is.close();
		} catch (Exception e) {
			System.err.println("Can't read the properties file. Make sure "
					+ filename + " is in the CLASSPATH");
			return null;
		}
		Enumeration enu = props.propertyNames();
		String value = "";
		while (enu.hasMoreElements()) {
			String name = (String) enu.nextElement();
			value = props.getProperty(name);
//			try {
//				value = new String(value.getBytes("ISO-8859-1"));
//			} catch (UnsupportedEncodingException e) {
//				System.err.println(getClass().getName() + ".getProp(String) error : " + e);
//			}
			props.setProperty(name, value);
		}
		return props;
	}
}
